package com.yoo.ons.view

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.makeramen.roundedimageview.RoundedImageView
import com.yoo.module_base.R
import com.yoo.module_base.utils.setOnClickCallback
import com.yoo.ons.listener.PictureManagerListener
import com.yoo.ons.model.PictureManagerModel


class PictureManagerView : FrameLayout {
    constructor(context: Context) : this(context = context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    private var showCoverBg: Boolean=true
    private var showSmallRadius: Boolean=true
    var photoSelectPosition = 0
    private var imageClickListener: PictureManagerListener? = null
    private var ivImageOne: RoundedImageView? = null
    private var ivImageTwo: RoundedImageView? = null
    private var ivImageThree: RoundedImageView? = null
    private var ivImageFour: RoundedImageView? = null
    private var ivImageFive: RoundedImageView? = null
    private var ivImageSix: RoundedImageView? = null
    private var ivImageOneClose: ImageView? = null
    private var ivImageTwoClose: ImageView? = null
    private var ivImageThreeClose: ImageView? = null
    private var ivImageFourClose: ImageView? = null
    private var ivImageFiveClose: ImageView? = null
    private var ivImageSixClose: ImageView? = null
    private var tvCover: TextView? = null
    private val imageViews = arrayListOf<RoundedImageView>()
    private val imageViewCloses = arrayListOf<ImageView>()
    var userImages = arrayListOf<PictureManagerModel>()

    fun initView(context: Context, attrs: AttributeSet?) {
        val view: View = LayoutInflater.from(context).inflate(R.layout.layout_picture_manager, this)
        val obtainStyledAttributes =
            context.obtainStyledAttributes(attrs, com.yoo.module_res.R.styleable.PictureManagerView)
        showCoverBg = obtainStyledAttributes.getBoolean(com.yoo.module_res.R.styleable.PictureManagerView_showCoverBg, false)
        showSmallRadius = obtainStyledAttributes.getBoolean(com.yoo.module_res.R.styleable.PictureManagerView_showRadius, false)

        ivImageOne = view.findViewById(R.id.ivImageOne)
        imageViews.add(ivImageOne!!)
        ivImageTwo = view.findViewById(R.id.ivImageTwo)
        imageViews.add(ivImageTwo!!)
        ivImageThree = view.findViewById(R.id.ivImageThree)
        imageViews.add(ivImageThree!!)
        ivImageFour = view.findViewById(R.id.ivImageFour)
        imageViews.add(ivImageFour!!)
        ivImageFive = view.findViewById(R.id.ivImageFive)
        imageViews.add(ivImageFive!!)
        ivImageSix = view.findViewById(R.id.ivImageSix)
        imageViews.add(ivImageSix!!)

        ivImageOneClose = view.findViewById(R.id.ivImageOneClose)
        imageViewCloses.add(ivImageOneClose!!)
        ivImageTwoClose = view.findViewById(R.id.ivImageTwoClose)
        imageViewCloses.add(ivImageTwoClose!!)
        ivImageThreeClose = view.findViewById(R.id.ivImageThreeClose)
        imageViewCloses.add(ivImageThreeClose!!)
        ivImageFourClose = view.findViewById(R.id.ivImageFourClose)
        imageViewCloses.add(ivImageFourClose!!)
        ivImageFiveClose = view.findViewById(R.id.ivImageFiveClose)
        imageViewCloses.add(ivImageFiveClose!!)
        ivImageSixClose = view.findViewById(R.id.ivImageSixClose)
        imageViewCloses.add(ivImageSixClose!!)

        tvCover = view.findViewById(R.id.tvCover)
        initListener()
        obtainStyledAttributes.recycle()
    }

    fun addImageClickListener(imageClickListener: PictureManagerListener) {
        this.imageClickListener = imageClickListener
    }

    private fun initListener() {
        imageViews.stream().forEach {
            it.setOnClickCallback {
                photoSelectPosition = it.tag as Int
                imageClickListener?.imageClick(true)
            }
        }
        imageViewCloses.stream().forEach { it ->
            it.setOnClickCallback {
                photoSelectPosition = it.tag as Int
                imageClickListener?.imageClick(false)
            }
        }
    }

    fun updateItemImage(position: Int) {
        if (position >= userImages.size) {
            return
        }
        val userPhotoBean = userImages[position]
        val url = if (TextUtils.isEmpty(userPhotoBean.imageLoadUrl)) {
            userPhotoBean.imageUrl
        } else {
            userPhotoBean.imageLoadUrl
        }
        val imageViewClose = imageViewCloses[position]
        val imageView = imageViews[position]
        if (!TextUtils.isEmpty(url)) {
            Glide.with(imageView)
                .load(url)
                .into(imageView)
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            imageViewClose.isVisible = position != 0
        } else {
            imageView.scaleType = ImageView.ScaleType.CENTER_INSIDE
            imageView.setImageResource(com.yoo.module_res.R.mipmap.img_add_picture)
            imageViewClose.isVisible = false
        }
    }

    /**
     * 初始化需要先设置集合
     */
    fun setData(userImages: ArrayList<PictureManagerModel>) {
        if (userImages.isEmpty()){
            return
        }
        this.userImages = userImages
        val residueNum = 6 - userImages.size
        for (num in 0 until residueNum) {
            userImages.add(PictureManagerModel("", "", "", false))
        }
        for (position in 0 until userImages.size) {
            val imageView = imageViews[position]
            val imageViewClose = imageViewCloses[position]
            imageView.tag = position
            updateItemImage(position)
            imageViewClose.tag = position
        }
    }

}